package com.sdg.server.utils;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class EsUtil {
    private Client client;

    public static void main(String[] args) throws UnknownHostException {

        //设置集群名称
        Settings settings = Settings.builder()
                //名字就是配置文件中的名字
                .put("cluster.name", "es-cluster")
                .build();
        //创建client,用java访问ES用的端口是9300
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddresses(new InetSocketTransportAddress(InetAddress.getByName("vm2"), 9300));
        //搜索数据（.actionGet()方法是同步的，没有返回就等待）
        GetResponse response = client.prepareGet("news", "fulltext", "1").execute().actionGet();
        //输出结果
        System.out.println(response);
        //关闭client
        client.close();
    }

}
